package org.linlinjava.litemall.db.service;

import org.linlinjava.litemall.db.dao.AgedmallCloudGoodsProductMapper;
import org.linlinjava.litemall.db.dao.AgedmallCloudHouseMapper;
import org.linlinjava.litemall.db.dao.app.AppCloudHouseMapper;
import org.linlinjava.litemall.db.domain.AgedmallCloudGoodsProduct;
import org.linlinjava.litemall.db.domain.AgedmallCloudGoodsProductExample;
import org.linlinjava.litemall.db.domain.AgedmallCloudHouse;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class AgedmallCloudHouseService {

    @Resource
    private AgedmallCloudHouseMapper agedmallCloudHouseMapper;

    @Resource
    private AppCloudHouseMapper appCloudHouseMapper;

    @Resource
    private AgedmallCloudGoodsProductMapper agedmallCloudGoodsProductMapper;

    /**
     * 获取含有此商品的云仓
     * @param goodsId
     * @param productId
     * @return
     */
    public List<Integer> getIdsByGoodsProduct(Integer goodsId, Integer productId) {
        return appCloudHouseMapper.getIdsByGoodsProduct(goodsId, productId);
    }

    public List<AgedmallCloudGoodsProduct> getCloudGoodsProductListByGoodsIds(List<Integer> goodsIdList) {
        if(goodsIdList == null || goodsIdList.size() == 0) {
            return new ArrayList<>();
        }
        List<Long> gList = new ArrayList<>();
        for(Integer gId:goodsIdList) {
            if(gId == null) {
                continue;
            }
            gList.add(Long.parseLong(gId.toString()));
        }
        if(gList.size() == 0) {
            return new ArrayList<>();
        }
        AgedmallCloudGoodsProductExample example = new AgedmallCloudGoodsProductExample();
        example.createCriteria().andGoodsIdIn(gList);
        return agedmallCloudGoodsProductMapper.selectByExample(example);
    }

    /**
     * 获取含有此商品的云仓
     * @param goodsId
     * @return
     */
    public List<Integer> getIdsByGoodsId(Integer goodsId) {
        return appCloudHouseMapper.getIdsByGoodsId(goodsId);
    }

    /**
     * 获取含有此商品的云仓，按距离排序
     * @param goodsId
     * @param productId
     * @param lng
     * @param lat
     * @return
     */
    public List<Integer> getIdsByGoodsProductAndLngLat(Integer goodsId, Integer productId, BigDecimal lng, BigDecimal lat) {
        return appCloudHouseMapper.getIdsByGoodsProductAndLngLat(goodsId, productId, lng, lat);
    }

    public AgedmallCloudHouse getById(Integer id) {
        return agedmallCloudHouseMapper.selectByPrimaryKey(id);
    }
}
